const initData = {
    antdList: JSON.parse(localStorage.getItem('antdList')) || [],
    siftList: [],
    allFlag: false
}

const antdReducer = (state = initData, {type, payload}) => {
    switch(type) {

        case 'ANTD_LIST':
            return {
                ...state,
                antdList: payload
            }

        case 'DETELE_ITEM':

            state.antdList.splice(payload, 1)

            localStorage.setItem('antdList', JSON.stringify(state.antdList))

            return {
                ...state,
                antdList: [...state.antdList]
            }

        case 'SAVE_BTN':

            state.antdList.forEach((item) => {
                if(item.id === payload.id) {
                    item.title = payload.Name
                    item.age = payload.Age
                    item.word = payload.Addredd
                }
            })

            localStorage.setItem('antdList', JSON.stringify(state.antdList))

            return {
                ...state,
                antdList: [...state.antdList]
            }

        case 'INQUIRE':
            
            state.siftList = state.antdList.filter(item => item.title.includes(payload))

            console.log(state.siftList)

            state.antdList = state.siftList

            return {
                ...state,
                antdList: [...state.antdList]
            }

        case 'INQUIRES':

            state.antdList = JSON.parse(localStorage.getItem('antdList'))

            return {
                ...state,
                antdList: [...state.antdList]
            }

        case 'ALL_FLAG':

            state.allFlag = !state.allFlag

            state.antdList.forEach(item => {
                item.flag = state.allFlag
            })

            return {
                ...state,
                antdList: [...state.antdList],
                allFlag: state.allFlag
            }

        case 'CLICK_FLAG':

            state.antdList[payload].flag = !state.antdList[payload].flag

            state.allFlag = state.antdList.every(item => item.flag)

            return {
                ...state,
                antdList: [...state.antdList],
                allFlag: state.allFlag
            }

        default:
            return {
                ...state
            }
    }
}

export default antdReducer